<template>
    <div>
        <div style="display: flex;flex-direction: row;justify-content: center">
            <el-input v-model="inputInfo" clearable style="width: 200px;margin-right: 10px" type="text"></el-input>
            <el-button icon="el-icon-search" size="medium" style="margin-left: 10px"
                       type="success" @click="handleSupplySearch">搜索
            </el-button>
            <el-button icon="el-icon-plus" size="medium"
                       style="margin-left: 100px" type="primary"
                       @click="handleSupplyAddition">新增
            </el-button>
        </div>
        <div style="display: flex;align-items: center;flex-direction: column;margin-left: -25px">
            <el-table :key="centerDialogVisible"
                      :data="supplies"
                      stripe
                      style="width: 90%">
                <el-table-column label="ID" prop="id" width="50px"></el-table-column>
                <el-table-column label="物资编号" prop="number"></el-table-column>
                <el-table-column label="物资名称" prop="name"></el-table-column>
                <el-table-column label="物资数量" prop="amount" width="100px"></el-table-column>
                <el-table-column label="物资类型" prop="type"></el-table-column>
                <el-table-column label="清点人" prop="head"></el-table-column>
                <el-table-column label="清点人联系方式" prop="headPhone"></el-table-column>
                <el-table-column label="清点时间" prop="time"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                :page-size="2"
                :total="paginationTotal"
                background
                layout="prev, pager, next"
                style="margin-top: 5px">
            </el-pagination>
        </div>
        <el-dialog
            :title="title"
            :visible.sync="centerDialogVisible"
            width="60%">
            <el-form :model="selectedSupply" class="selectedSupplyForm">
                <el-form-item label="物资名称:" prop="name">
                    <el-input v-model="selectedSupply.name" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="物资数量:" prop="amount">
                    <el-input v-model="selectedSupply.amount" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="物资编号:" prop="number">
                    <el-input v-model="selectedSupply.number" disabled></el-input>
                </el-form-item>

                <el-form-item label="清点人:" prop="head">
                    <el-input v-model="selectedSupply.head" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="清点人联系方式:" prop="headPhone">
                    <el-input v-model="selectedSupply.headPhone"></el-input>
                </el-form-item>

                <el-form-item label="物资类型:" prop="type">
                    <el-select v-model="selectedSupply.type">
                        <el-option
                            v-for="item in supplyOptions"
                            :key="item.label"
                            :label="item.label"
                            :value="item.label">
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                    <el-button @click="centerDialogVisible = false;">取 消</el-button>
                    <el-button type="primary" @click="handleDialogEnter">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import variables from "@/assets/variables";

export default {
    name: "SupplyMaintenance",
    data() {
        return {
            supplies: [],
            title: "",
            inputInfo: "",
            centerDialogVisible: false,
            selectedSupply: {},
            selectedSupplyIndex: -1,
            supplyOptions: [
                {
                    label: "生命救助",
                },
                {
                    label: "器材工具",
                }
            ]
        }
    },
    methods: {
        handleSupplyAddition() {
            this.title = "新增";
            this.centerDialogVisible = true;
            this.selectedSupply = {
                id: this.supplies.length + 1,
                number: new Date().getTime().toString(),
            };
            console.log(new Date())
        },
        handleSupplySearch() {
            if (this.inputInfo === "") {
                this.supplies = variables.supplies;
            } else {
                this.supplies = variables.supplies.filter(supply =>
                    supply.name.includes(this.inputInfo) ||
                    supply.amount.includes(this.inputInfo) ||
                    supply.type.includes(this.inputInfo) ||
                    supply.head.includes(this.inputInfo) ||
                    supply.headPhone.includes(this.inputInfo) ||
                    supply.number.includes(this.inputInfo)
                );
            }
        },
        handleEdit(index, obj) {
            this.title = "修改";
            this.centerDialogVisible = true;
            this.selectedSupplyIndex = index;
            this.selectedSupply = JSON.parse(JSON.stringify(obj));
        },
        handleDelete(index) {
            this.supplies.splice(index, 1);
            this.$message({
                type: "success",
                message: "删除成功",
            })
        },
        handleDialogEnter() {
            if (this.title === "新增") {
                this.supplies.push(this.selectedSupply);
            } else {
                this.supplies[this.selectedSupplyIndex] = this.selectedSupply;
            }
            this.centerDialogVisible = false;
        }

    },
    mounted() {
        this.supplies = variables.supplies
    },
    computed: {
        paginationTotal() {
            return this.supplies.length;
        }
    }
}
</script>

<style scoped>
.selectedSupplyForm {
    height: 300px;
}

.selectedSupplyForm >>> .el-form-item {
    display: inline-block;
    width: 260px;
    float: left;
    margin: 5px 15px;
}
</style>